package myky.com.doctorapp.ui

import android.app.Activity
import android.content.Intent
import android.support.v4.content.ContextCompat
import android.view.View
import android.widget.Button
import android.widget.ImageView
import com.gyf.barlibrary.ImmersionBar
import kotlinx.android.synthetic.main.include_toolbar_white.*
import myky.com.doctorapp.R
import myky.com.doctorapp.base.BaseActivity
import myky.com.doctorapp.contract.ChangePassWordContract
import myky.com.doctorapp.presenter.ChangePassWordPresenter
import myky.com.doctorapp.utils.*
import myky.com.doctorapp.wight.AddAndSubEditText
import myky.com.doctorapp.wight.SelfDialog


/**
 * Description: 修改密码
 * @author xiangyao
 * Created by 2019/3/13 11:07 AM
 */

class ChangePasswordActivity : BaseActivity<ChangePassWordPresenter, String>(), ChangePassWordContract.View {


    private val editTexts = arrayOfNulls<AddAndSubEditText>(3)
    private val imageViews = arrayOfNulls<ImageView>(3)
    private val mEditTextId = intArrayOf(R.id.pwd_update_et1, R.id.pwd_update_et2, R.id.pwd_update_et3)
    private val imageId = intArrayOf(R.id.pwd_update_iv1, R.id.pwd_update_iv2, R.id.pwd_update_iv3)
    lateinit var button: Button
    private lateinit var mImageView: ImageView
    lateinit var selfDialog: SelfDialog

    override fun changePwdSuccesed() {

        selfDialog = SelfDialog(this@ChangePasswordActivity)
        selfDialog.setTitle("修改成功,请重新登陆")

        selfDialog.setYesOnclickListener("确定", SelfDialog.onYesOnclickListener {
            selfDialog.dismiss()

            mPresenter.plogout()
        })


        selfDialog.show()
    }

    override fun reloadActivity() {
    }

    override fun injectTarget(): View = findViewById(R.id.rootLayout)

    override fun initData() {

    }

    override fun initView() {


        ImmersionBar.with(this).barColorInt(ContextCompat.getColor(this,R.color.colorPrimary)).titleBar(R.id.titleLayout).navigationBarEnable(false).init()

        button = findViewById(R.id.pwd_update_btn)
        mImageView = findViewById(R.id.act_pwd_update_iv)
        for (i in 0..2) {
            editTexts[i] = findViewById(mEditTextId[i])
            imageViews[i] = findViewById(imageId[i])
        }


        //监听输入不为空
        val textChangeListener = AddAndSubEditText.textChangeListener(button)
        textChangeListener.addAllEditText(editTexts)

        //设置密码可见监听
        for (i in editTexts.indices) {
            editTexts[i]?.setPassWordVisible(imageViews[i])
        }
        button.isEnabled = textChangeListener.checkAllEdit()
        button.setOnClickListener(View.OnClickListener {
            button.isEnabled = false
            val password = editTexts[1]?.text.toString()
            val againpassword = editTexts[2]?.text.toString()
            if (password != againpassword) {
                ToastUtils.show("两次新密码输入不一致")
                button.isEnabled = true
                return@OnClickListener
            }
            if (CheckText.isPass_Word(password)) {

                mPresenter.upPassWord(editTexts[0]?.text.toString(), editTexts[1]?.text.toString(), editTexts[2]?.text.toString())
            }

        })
        mImageView.setOnClickListener { finish() }

    }

    override fun logout() {

        var intent = Intent()

        intent.putExtra("IS_LOGOUT", "IS_LOGOUT")

        setResult(Activity.RESULT_OK, intent)

        ShareUtil.putData(Constants.ISLOGIN,false)

        finish()
    }


    override fun changePwdFailed(message: String?) {
        button.isEnabled = true
        ToastUtils.show("服务异常")


    }

    override fun setButtonEnable() {
        button.isEnabled = true
    }

    override fun getContenView(): Int = R.layout.activity_change_password

    override fun showDataSuccess(datas: String?) {
    }

    override fun getPresenter(): ChangePassWordPresenter {
        return ChangePassWordPresenter()
    }

}
